package com.easy.controller;

import com.easy.bean.Department;
import com.easy.bean.Doctor;
import com.easy.common.CommonResult;
import com.easy.dao.IDepartmentDao;

import com.easy.service.IDepartmentService;

import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class DepartmentController {
    @Autowired
    IDepartmentDao dao;
    @Autowired
    IDepartmentService dtS;

    @GetMapping("department")
    public CommonResult getDepartment(Department department, Page page){
        List<Department> list=dtS.getDepartment(department,page);
        int count=dtS.getCount(department);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);

    }
    @PostMapping("department")
    public CommonResult addDepartmentJSON(@RequestBody Department department){
        dtS.addDepartment(department);
        department=dtS.getDepartmentById(department.getId());
        return CommonResult.success(department);
    }

    @DeleteMapping("department/{id}")
    public CommonResult delDepartment(@PathVariable int id){
        int count=dao.delDepartment(id);
        if(count>0){
            return CommonResult.success();
        }else {
            return CommonResult.fail();
        }
    }
    @PutMapping("department")
    public CommonResult editDepartmentJSON(@RequestBody Department department ){
        dtS.editDepartment(department);
        department=dtS.getDepartmentById(department.getId());
        return CommonResult.success(department);
    }
}
